package com.example.peibei.ui.weight;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NickNameDialog extends Dialog{
    View back,ensure;

    EditText content;
    Context context;
    String nickname;
    public NickNameDialog(Context context,String nickname) {
        super(context, R.style.Theme_dialog);
        this.context=context;
        this.nickname=nickname;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_nickname_layout);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //初始化界面控件
        initView();
        //初始化界面控件的事件
        initEvent();
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        ensure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( onClickBottomListener!= null) {
                    if(TextUtils.isEmpty(content.getText()))
                    {
                        UIUtils.showToastSafe("昵称不能为空");
                        return;
                    }else
                    {
                        onClickBottomListener.onContent(content.getText().toString().trim());
                        dismiss();
                    }

                }
            }
        });
    }

    @Override
    public void show() {
        super.show();
    }
    /**
     * 初始化界面控件
     */
    private void initView() {
        back =  findViewById(R.id.back);
        ensure = findViewById(R.id.ensure);
        content = findViewById(R.id.content);
        content.setText(nickname);
        InputFilter filter=new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (content.getText().length() >= 8){
                    return "";
                }
                String speChat="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
                Pattern pattern = Pattern.compile(speChat);
                Matcher matcher = pattern.matcher(source.toString());
                if(matcher.find())return "";
                else return null;
            }
        };
        content.setFilters(new InputFilter[]{filter});

    }

    /**
     * 设置确定取消按钮的回调
     */
    public OnClickBottomListener onClickBottomListener;
    public NickNameDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener) {

        this.onClickBottomListener = onClickBottomListener;
        return this;
    }
    public interface OnClickBottomListener{

        void onContent(String nickname);

    }
}
